
#include "new_game_wizard.h"


NewGameWizard::NewGameWizard(QWidget *parent)
{
  this->parent = parent;
  addPage(&this->galaxyPage);
  addPage(&this->racePage);
  addPage(&this->ambiencePage);

  connect(&this->ambiencePage, SIGNAL(ambienceEnd())
	 , this, SLOT(wizardEnd()));
  setWindowTitle(tr("New game wizard"));
}

NewGameWizard::~NewGameWizard()
{
  
}

void		NewGameWizard::setGameParams(GameParams *gameParams)
{
  this->gameParams = gameParams;

  this->galaxyPage.setGameParams(gameParams);
  this->racePage.setGameParams(gameParams);
  this->ambiencePage.setGameParams(gameParams);
}

void		NewGameWizard::wizardEnd()
{
  emit makeNewGameSig();
}

NewGameWizardPGalaxy::NewGameWizardPGalaxy(QWidget *parent)
{  
  this->parent = parent;
  QGridLayout	*mainLayout = new QGridLayout;
  QHBoxLayout	*textLayout = new QHBoxLayout;

  galaxyPreview = new GalaxyPreview(this);  
  setTitle(tr("Galaxy"));
  setLayout(mainLayout);
  mainLayout->addWidget(galaxyPreview, 1, 1, 2, 2);
  
  this->nbStars = 2;
  // create slider
  this->nbStarSlider = new QSlider(Qt::Horizontal, this);
  nbStarSlider->setRange(2, 200);
  this->nbStarSlider->setSingleStep(1);
  this->nbStarSlider->setPageStep(15);
  this->nbStarSlider->setTickInterval(15);
  mainLayout->addWidget(this->nbStarSlider, 3, 1, 1, 2);
  connect(this->nbStarSlider, SIGNAL(valueChanged(int))
	  , galaxyPreview, SLOT(changeNbStar(int)));
  
  connect(this->nbStarSlider, SIGNAL(valueChanged(int))
	  , this, SLOT(changeNbStar(int)));
  
  this->timer = new QTimer(this);
  connect(timer, SIGNAL(timeout()), galaxyPreview, SLOT(animate()));
  timer->start(10);

  labelText = new QLabel(tr("Number of stars in the galaxy : "), this);
  textLayout->addWidget(labelText);
  labelNbStars = new QLabel;
  labelNbStars->setNum(2);
  textLayout->addWidget(labelNbStars);
  connect(this->nbStarSlider, SIGNAL(valueChanged(int)), 
	  labelNbStars, SLOT(setNum(int)));
  mainLayout->addLayout(textLayout, 4, 1, 1, 1);
}

NewGameWizardPGalaxy::~NewGameWizardPGalaxy()
{
  this->timer->stop();
  //delete this->timer;
}

void		NewGameWizardPGalaxy::setGameParams(GameParams *gameParams)
{
  this->gameParams = gameParams;
}

void		NewGameWizardPGalaxy::changeNbStar(int newNb)
{
  this->nbStars = newNb;
}

bool		NewGameWizardPGalaxy::validatePage()
{
  this->gameParams->setStarDensity(this->nbStars);
  return true;
}

NewGameWizardPRace::NewGameWizardPRace(QWidget *parent)
{
  QGridLayout	*mainLayout = new QGridLayout;
  QGridLayout	*textLayout;
  QLabel	*tmp;
  char		buffer[64], labelBuff[32];
  std::string	str;
  Races		raceList;
  QPushButton	*previous, *next, *setPlayer, *addEnemy;
  int		i;

  this->parent = parent;
  this->playerRace = -1;
  setTitle(tr("Races"));
  setLayout(mainLayout);
  
  this->currentRace = 0;
  this->playerRace = -1;
  textLayout = new QGridLayout;
  tmp = new QLabel(tr("Player race : "));
  textLayout->addWidget(tmp, 0, 0);
  this->playerPic = new QLabel;
  textLayout->addWidget(this->playerPic, 0, 1);
  this->player = new QLabel;
  this->player->setStyleSheet("font :  14px; color : green");
  textLayout->addWidget(this->player, 0, 2);
  
  for(i = 0; i < 5; i++)
    {
      sprintf(labelBuff, "Ennemy %i : ", i + 1);
      tmp = new QLabel(tr(labelBuff));
      textLayout->addWidget(tmp, i + 1, 0);
      tmp = new QLabel;
      this->enemyPic.push_back(tmp);
      textLayout->addWidget(this->enemyPic.at(i), i + 1, 1);
      tmp = new QLabel;
      tmp->setStyleSheet(raceList.getEnemyColorStyle(i));
      this->enemyName.push_back(tmp);
      textLayout->addWidget(this->enemyName.at(i), i + 1, 2);
      
    }
  
  // skip a line
  this->delLastEnnemy = new QPushButton(tr("Remove last ennemy"), this);
  textLayout->addWidget(this->delLastEnnemy, 6, 2, 1, 2);
  connect(this->delLastEnnemy, SIGNAL(pressed()), this, SLOT(removeLastEnemy()));

  // create buttons
  previous = new QPushButton(tr("Previous"), this);
  textLayout->addWidget(previous, 7, 0);
  connect(previous, SIGNAL(pressed()), this, SLOT(previousRace()));
  next = new QPushButton(tr("Next"), this);
  textLayout->addWidget(next, 7, 1);
  connect(next, SIGNAL(pressed()), this, SLOT(nextRace()));
  setPlayer = new QPushButton(tr("Set as player race"), this);
  textLayout->addWidget(setPlayer, 7, 2);
  connect(setPlayer, SIGNAL(pressed()), this, SLOT(setPlayerRace()));
  addEnemy = new QPushButton(tr("Add to ennemy race"), this);
  connect(addEnemy, SIGNAL(pressed()), this, SLOT(addEnemy()));
  textLayout->addWidget(addEnemy, 7, 3);
  
  // display the race data
  sprintf(buffer, "../img/races/%i.png", this->currentRace);  
  QPixmap currentRacePixmap(tr(buffer));
  currentRacePixmap = currentRacePixmap.scaledToHeight(128
						       , Qt::SmoothTransformation);
  this->currentRacePicture = new QLabel;
  this->currentRacePicture->setPixmap(currentRacePixmap);
  textLayout->addWidget(this->currentRacePicture, 8, 0, 2, 2);
  this->currentRaceName = new QLabel(raceList.getRaceName(this->currentRace));
  this->currentRaceName->setStyleSheet("font : bold 14px");
  textLayout->addWidget(this->currentRaceName, 8, 2, 1, 2);
  this->currentRaceData = new QLabel(raceList.getRaceDescription(this->currentRace));
  this->currentRaceData->setWordWrap(true);
  textLayout->addWidget(this->currentRaceData, 9, 2, 1, 2);

  mainLayout->addLayout(textLayout, 0, 0);
}

void		NewGameWizardPRace::setGameParams(GameParams *gameParams)
{
  this->gameParams = gameParams;
}

void		NewGameWizardPRace::removeLastEnemy()
{
  int		curEnemy;
  Races		raceList;
  
  curEnemy = this->enemyId.size();
  if(curEnemy > 0)
    {
      this->enemyId.pop_back();
      emit this->enemyName.at(curEnemy - 1)
	->setText((tr("")));
      emit this->enemyPic.at(curEnemy - 1)
	->setPixmap(0);
      // free the enxt button
      emit allowNextPage();
    }
}

void		NewGameWizardPRace::setPlayerRace()
{
  char		buffer[64];
  Races		raceList;
  int		i;
  bool		noDouble;
  
  for(i = 0, noDouble = true; i < (int)this->enemyId.size(); i++)
    if(this->enemyId.at(i) == this->currentRace)
      noDouble = false;
  if(noDouble == true)
    {  
      this->playerRace = this->currentRace;
      emit this->player->setText(raceList.getRaceName(this->currentRace));
      sprintf(buffer, "../img/races/%i.png", this->currentRace);  
      QPixmap currentRacePixmap(tr(buffer));
      currentRacePixmap = currentRacePixmap.scaledToHeight(32
							   , Qt::SmoothTransformation);
      emit this->playerPic->setPixmap(currentRacePixmap);
    }
}

void		NewGameWizardPRace::addEnemy()
{
  int		curEnemy;
  char		buffer[64];
  Races		raceList;
  int		i;
  bool		noDouble;

  curEnemy = this->enemyId.size();
  if(curEnemy < 5 && this->playerRace != this->currentRace)
    {
      for(i = 0, noDouble = true; i < curEnemy; i++)
	if(this->enemyId.at(i) == this->currentRace)
	  noDouble = false;
      if(noDouble == true)
	{
	  this->enemyId.push_back(this->currentRace);
	  sprintf(buffer, "../img/races/%i.png", this->currentRace);
	  QPixmap currentRacePixmap(tr(buffer));
	  currentRacePixmap = currentRacePixmap.scaledToHeight(32
							       , Qt::SmoothTransformation);
	  emit this->enemyPic.at(curEnemy)
	    ->setPixmap(currentRacePixmap);
	  emit this->enemyName.at(curEnemy)
	    ->setText(raceList.getRaceName(this->currentRace));
	}
    }
}

void		NewGameWizardPRace::nextRace()
{
  this->currentRace++;
  if(this->currentRace >= RACE_SIZE)
    this->currentRace = 0;
  
  this->redrawRaceDesc();
}

void		NewGameWizardPRace::previousRace()
{
  this->currentRace--;
  if(this->currentRace < 0)
    this->currentRace =  RACE_SIZE - 1;
  this->redrawRaceDesc();
}

void		NewGameWizardPRace::redrawRaceDesc()
{
  char		buffer[64];
  Races		raceList;

  emit this->currentRaceName->setText(raceList.getRaceName(this->currentRace));
  emit this->currentRaceData->setText(raceList.getRaceDescription(this->currentRace));
  
  sprintf(buffer, "../img/races/%i.png", this->currentRace);  
  QPixmap currentRacePixmap(tr(buffer));
  currentRacePixmap = currentRacePixmap.scaledToHeight(128
						       , Qt::SmoothTransformation);
  emit this->currentRacePicture->setPixmap(currentRacePixmap);
}

bool		NewGameWizardPRace::isComplete()
{
  if(this->enemyId.size() > 0
     && this->playerRace >= 0)
    return(true);
  return(false);
}

bool		NewGameWizardPRace::validatePage()
{
  if(!this->isComplete())
    return false;
  this->gameParams->setPlayerRace(this->playerRace);
  this->gameParams->setEnemyList(this->enemyId);
  return true;
}

NewGameWizardPAmbience::NewGameWizardPAmbience(QWidget *parent)
{ 
  QPixmap	imgExplosive(NGWIZARD_IMG_EXPLOSIVE);
  QLabel	*labExplosive;
  QRadioButton	*raEx;

  QPixmap	imgNeutral(NGWIZARD_IMG_NEUTRAL);
  QLabel	*labNeutral;
  QRadioButton	*raNe;

  QPixmap	imgPeace(NGWIZARD_IMG_PEACEFULL);
  QLabel	*labPeace;
  QRadioButton	*raPe;
  
  QGridLayout	*mainLayout = new QGridLayout;
  QGridLayout	*layoutBox;
  QLabel	*desc;

  this->parent = parent;

  setTitle(tr("Ambience"));
  setLayout(mainLayout);

  raEx = new QRadioButton(tr("Explosive"), this);
  raEx->setStyleSheet("font : bold 14px");
  mainLayout->addWidget(raEx, 1, 1);
  layoutBox = new QGridLayout;
  imgExplosive = imgExplosive.scaledToHeight(64, Qt::SmoothTransformation);
  labExplosive = new QLabel;
  labExplosive->setPixmap(imgExplosive);
  layoutBox->addWidget(labExplosive, 1, 1);
  desc = new QLabel(tr("The world is on fire The world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fire"));
  desc->setWordWrap(true);
  layoutBox->addWidget(desc, 1, 2);
  mainLayout->addLayout(layoutBox, 1, 2);

  raNe = new QRadioButton(tr("Neutral"), this);
  raNe->setStyleSheet("font : bold 14px");
  mainLayout->addWidget(raNe, 2, 1);
  layoutBox = new QGridLayout;
  imgNeutral = imgNeutral.scaledToHeight(64, Qt::SmoothTransformation);
  labNeutral = new QLabel;
  labNeutral->setPixmap(imgNeutral);
  layoutBox->addWidget(labNeutral, 1, 1);
  desc = new QLabel(tr("The world is on fire The world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fire"));
  desc->setWordWrap(true);
  layoutBox->addWidget(desc, 1, 2);
  mainLayout->addLayout(layoutBox, 2, 2);
  
  raPe = new QRadioButton(tr("Peacefull"), this);
  raPe->setStyleSheet("font : bold 14px");
  mainLayout->addWidget(raPe, 3, 1);
  layoutBox = new QGridLayout;
  imgPeace = imgPeace.scaledToHeight(64, Qt::SmoothTransformation);
  labPeace = new QLabel;
  labPeace->setPixmap(imgPeace);
  layoutBox->addWidget(labPeace, 1, 1);
  desc = new QLabel(tr("The world is on fire The world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fireThe world is on fire"));
  desc->setWordWrap(true);
  layoutBox->addWidget(desc, 1, 2);
  mainLayout->addLayout(layoutBox, 3, 2);
}

void		NewGameWizardPAmbience::setGameParams(GameParams *gameParams)
{
  this->gameParams = gameParams;
}

bool		NewGameWizardPAmbience::validatePage()
{
  this->gameParams->setAmbience(1);
  emit ambienceEnd();
  return true; 
}
